package com.liuqi.loggers.facade;

/**
 * 日志适配器,
 *
 * @author liuqi
 * @date 2024/12/13 14:19
 **/
public class LoggersFacade {

    private static boolean isSFlf4j;

    static {
        try {
            Class.forName("org.slf4j.Logger");
            isSFlf4j = true;
        } catch (ClassNotFoundException e) {
            isSFlf4j = false;
        }
    }

    public static final com.liuqi.loggers.api.Logger getLogger(Class<?> clazz) {
        return getLogger(clazz.getName());
    }

    public static final com.liuqi.loggers.api.Logger getLogger(String name) {
        if (isSFlf4j) {
            return new SFlf4jLogger(org.slf4j.LoggerFactory.getLogger(name));
        } else {
            return com.liuqi.loggers.api.LoggerFactory.getLogger(name);
        }
    }

    private static class SFlf4jLogger implements com.liuqi.loggers.api.Logger {

        private final org.slf4j.Logger logger;

        public SFlf4jLogger(org.slf4j.Logger logger) {
            this.logger = logger;
        }

        @Override
        public void trace(String message) {
            logger.trace(message);
        }

        @Override
        public void trace(String format, Object... arguments) {
            logger.trace(format, arguments);
        }

        @Override
        public void trace(String message, Throwable throwable) {
            logger.trace(message, throwable);
        }

        @Override
        public void debug(String message) {
            logger.debug(message);
        }

        @Override
        public void debug(String format, Object... arguments) {
            logger.debug(format, arguments);
        }

        @Override
        public void debug(String message, Throwable throwable) {
            logger.debug(message, throwable);
        }

        @Override
        public void info(String message) {
            logger.info(message);
        }

        @Override
        public void info(String format, Object... arguments) {
            logger.info(format, arguments);
        }

        @Override
        public void info(String message, Throwable throwable) {
            logger.info(message, throwable);
        }

        @Override
        public void warn(String message) {
            logger.warn(message);
        }

        @Override
        public void warn(String format, Object... arguments) {
            logger.warn(format, arguments);
        }

        @Override
        public void warn(String message, Throwable throwable) {
            logger.warn(message, throwable);
        }

        @Override
        public void error(String message) {
            logger.error(message);
        }

        @Override
        public void error(String format, Object... arguments) {
            logger.error(format, arguments);
        }

        @Override
        public void error(String message, Throwable throwable) {
            logger.error(message, throwable);
        }

        @Override
        public boolean isTraceEnabled() {
            return logger.isTraceEnabled();
        }

        @Override
        public boolean isDebugEnabled() {
            return logger.isDebugEnabled();
        }

        @Override
        public boolean isInfoEnabled() {
            return logger.isInfoEnabled();
        }

        @Override
        public boolean isWarnEnabled() {
            return logger.isWarnEnabled();
        }

        @Override
        public boolean isErrorEnabled() {
            return logger.isErrorEnabled();
        }
    }

}
